/**
  ******************************************************************************
  * @file    Libraries/Device/TS32Fx/TS32Fx_LL_Driver/inc/ts32fx_ll_comp.h
  * @author  TOPSYS Application Team
  * @version V1.0.0
  * @date    02-11-2018
  * @brief   This file contains all the comp LL firmware functions.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2018 TOPSYS</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 
  
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TS32FX_LL_COMP_H
#define __TS32FX_LL_COMP_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "ts32fx.h"
     
/** @addtogroup TS32Fx_StdPeriph_Driver TS32Fx Driver
  * @{
  */
     
/** @addtogroup comp_interface_gr COMP Driver
  * @ingroup  TS32Fx_StdPeriph_Driver
  * @{
  */ 

/** @addtogroup COMP_LL_Driver COMP LL Driver
  * @ingroup  comp_interface_gr
  * @brief Mainly the driver part of the COMP module, which includes \b COMP \b Register 
  * \b Constants, \b COMP \b Exported \b Constants, \b COMP \b Exported \b Struct, \b COMP
  * \b Data \b transfers \b functions, \b COMP \b Initialization \b and \b COMP \b Configuration 
  * \b And \b Interrupt \b Handle \b function.
  * @{
  */

/* Exported types ------------------------------------------------------------*/

/* Exported constants --------------------------------------------------------*/

/** @defgroup COMP_LL_Register_Constants COMP LL Register Constants
  * @ingroup  COMP_LL_Driver
  * @brief    COMP LL register constant table definition
  *
  *
@verbatim   
  ===============================================================================
                                Register Constants
  ===============================================================================  
  
    Register Constants mainly encapsulates each bit in each group in the COMP 
    register. In the process of configuration, the macro definition can be directly 
    called to configure the COMP register, mainly for convenience. Understand the 
    configuration of the COMP.
    
@endverbatim
  *
  * @{
  */


/***** COMP CON0 Register *****/
/*! RW, The CON0 Register is lock(operate once only)  
 * 0 : unlock  
 * 1 : lock  
 */
#define LL_COMP_CON0_LOCK                             ((1UL) << 31)
/*!RW, Used to indicate that comparator0 ie enable or disable.  
 * 00: reserved  
 * 01: comparator 0 rising edge wakes up  
 * 10: comparator 0 drops edge wake up  
 * 11: both the rising edge and the falling edge of the comparator can be awakened  
 */
#define LL_COMP_CON0_WKUP_SEL(n)                      (((n)&0x03) << 17)

/*!RW, Comparator0 output filter cycle selection  
 */
#define LL_COMP_CON0_FILT_NUM(n)                      (((n)&0x1F) << 12)

/*!RW, Comparator0 voltage hysteresis selection  
 * 00: None  
 * 01: 22 * 2 mv  
 * 10: 34 * 2 mv  
 * 11: 42 * 2 mv  
 */
#define LL_COMP_CON0_HY_SEL(n)                        (((n)&0x03) << 10)

/*!RW, Comparator0 positive input selection  
 * 00 : DAC_OUT  
 * 01 : PC2  
 * 10 : PC3  
 * 11 : PC4  
 */
#define LL_COMP_CON0_CH_PSEL(n)                       (((n)&0x03) << 8)

/*!RW, Comparator0 negative pole selection  
 * 00 : PA3  
 * 01 : PA2  
 * 10 : PA1  
 * 11 : PA0  
 */
#define LL_COMP_CON0_CH_NSEL(n)                       (((n)&0x03) << 6)

/*!RW, VIN/VIP output enable of comparator0
 */
#define LL_COMP_CON0_POEN                             ((1UL) << 5)

/*!RW, Comparator0 low voltage enable signal, high level effective  
 */
#define LL_COMP_CON0_LOWP                             ((1UL) << 4)

/*!RW, Invert the comparator0 output result enable
 */
#define LL_COMP_CON0_INV_EN                           ((1UL) << 3)

/*!RW, The comparator0 wakeup function enable
 */
#define LL_COMP_CON0_WKUP_EN                          ((1UL) << 2)

/*!RW, The comparator0 interrupt enable
 */
#define LL_COMP_CON0_IE                               ((1UL) << 1)

/*!RW, The comparator0 enable
 */
#define LL_COMP_CON0_EN                               ((1UL) << 0)

/***** COMP CON1 Register *****/
/*! RW, The CON1 Register is lock(operate once only)  
 * 0 : unlock  
 * 1 : lock  
 */
#define LL_COMP_CON1_LOCK                             ((1UL) << 31)

/*!RW, Used to indicate that comparator1 ie enable or disable.  
 * 00: reserved  
 * 01: comparator 1 rising edge wakes up  
 * 10: comparator 1 drops edge wake up  
 * 11: both the rising edge and the falling edge of the comparator can be awakened  
 */
#define LL_COMP_CON1_WKUP_SEL(n)                      (((n)&0x03) << 17)

/*!RW, Comparator1 output filter cycle selection
 */
#define LL_COMP_CON1_FILT_NUM(n)                      (((n)&0x1F) << 12)

/*!RW, Invert the comparator1 output result enable
 */
#define LL_COMP_CON1_INV_EN                           ((1UL) << 3)

/*!RW, The comparator1 wakeup function enable
 */
#define LL_COMP_CON1_WKUP_EN                          ((1UL) << 2)

/*!RW, The comparator1 interrupt enable
 */
#define LL_COMP_CON1_IE                               ((1UL) << 1)

/*!RW, The comparator1 enable
 */
#define LL_COMP_CON1_EN                               ((1UL) << 0)

/***** COMP STA Register *****/
/*!RO, The comparator1 output pending  
 * Write 1 to clear  
 */
#define LL_COMP_STA_COMP1_PENDING                     ((1UL) << 1)

/*!RO, The comparator0 output pending  
 * Write 1 to clear  
 */
#define LL_COMP_STA_COMP0_PENDING                     ((1UL) << 0)

/****** ADKEY STA ****************/
/*RW, Comparator1 negative pole selection (note : this register is used to ADKEY_STA register )  
 */
#define LL_COMP_CON1_CH_NSEL(n)                       (((n)&0x3F) << 4)

/**
  * @}
  */

/** @defgroup COMP_LL_Exported_Constants COMP LL Exported Constants
  * @ingroup  COMP_LL_Driver
  * @brief    COMP LL external constant definition
  *
@verbatim   
  ===============================================================================
                                Exported Constants
  ===============================================================================  
  
    Exported Constants mainly restricts the partial configuration of the abstraction 
    layer by using the form of enumeration to facilitate the use and understanding of 
    the module configuration. For the specific enumeration meaning, please refer to 
    the annotation of each module.

@endverbatim
  *
  * @{
  */
  
/***** DRIVER API *****/



/***** LL API *****/
  

/**
  * @brief COMP wake_up mode enum type, consistent with the spec definition.
  */
typedef enum {
    /*! comparator rising edge will wakeup or interrupt chip.
     */
    LL_COMP_WAKEUP_OR_IRQ_RISING_EDGE         = 1,
    /*! comparator falling edge will wakeup or interrupt chip.
     */
    LL_COMP_WAKEUP_OR_IRQ_FALLING_EDGE        = 2,
    /*! Both comparator rising edge and falling edge will wakeup chip or interrrupt.
     */   
    LL_COMP_WAKEUP_OR_IRQ_BOTH_EDGE           = 3,
} TYPE_ENUM_LL_COMP_WAKEUP_OR_IRQ_MODE;

/**
  * @brief comparator hysteresis enum type, consistent with the spec definition.
  */
typedef enum {
    /*! Comparator voltage hysteresis is none
     */
    LL_COMP0_HY_NONE                          = 0,
    /*! Comparator voltage hysteresis is 22*2mv
     */
    LL_COMP0_HY_44MV                          = 1,
    /*! Comparator voltage hysteresis is 34*2mv
     */   
    LL_COMP0_HY_68MV                          = 2,
    /*! Comparator voltage hysteresis is 42*2mv
     */   
    LL_COMP0_HY_84MV                          = 3,
    
} TYPE_ENUM_LL_COMP0_HY_SEL;

/**
  * @brief comparator0 positive polarity selection enum type, consistent with the spec definition.
  */
typedef enum {
    /*! Comparator0 negative input selection is PA3
     */
    LL_COMP0_NEGATIVE_SEL_PA3                = 0,
    /*! Comparator0 negative input selection is PA2
     */
    LL_COMP0_NEGATIVE_SEL_PA2                = 1,
    /*! Comparator0 negative input selection is PA1
     */   
    LL_COMP0_NEGATIVE_SEL_PA1                = 2,
    /*! Comparator0 negative input selection is PA0
     */   
    LL_COMP0_NEGATIVE_SEL_PA0                = 3,
    
} TYPE_ENUM_LL_COMP0_NEGATIVE_SEL;

/**
  * @brief comparator0 negative polarity selection mode enum type, consistent with the spec definition.
  */
typedef enum {
    /*! Comparator0 positive input selection is DACOUT
     */
    LL_COMP0_POSITIVE_SEL_DACOUT             = 0,
    /*! Comparator0 positive input selection is PC2
     */
    LL_COMP0_POSITIVE_SEL_PC2                = 1,
    /*! Comparator0 positive input selection is PC3
     */   
    LL_COMP0_POSITIVE_SEL_PC3                = 2,
    /*! Comparator0 positive input selection is PC4
     */   
    LL_COMP0_POSITIVE_SEL_PC4                = 3,
    
} TYPE_ENUM_LL_COMP0_POSITIVE_SEL;

/**
  * @brief comparator0 negative polarity selection mode enum type, consistent with the spec definition.
  */
typedef enum {
    /*! Comparator0 selected
     */
    LL_COMP_CH0                              = 0,
    /*! Comparator1 selected
     */
    LL_COMP_CH1                              = 1,
    
} TYPE_ENUM_LL_COMP_CH;


/**
  * @brief comparator1 positive polarity selection mode enum type, consistent with the spec definition.
  */
typedef enum {
    /*! Comparator1 positive input selection is PC0
     */
    LL_COMP1_POSITIVE_SEL_PC0                = 0,
    /*! Comparator1 positive input selection is PC1
     */
    LL_COMP1_POSITIVE_SEL_PC1                = 1,
    /*! Comparator1 positive input selection is PC2
     */   
    LL_COMP1_POSITIVE_SEL_PC2                = 2,
    /*! Comparator1 positive input selection is PC3
     */   
    LL_COMP1_POSITIVE_SEL_PC3                = 3,
    /*! Comparator1 positive input selection is PC6
     */
    LL_COMP1_POSITIVE_SEL_PC6                = 4,
    /*! Comparator1 positive input selection is PC7
     */
    LL_COMP1_POSITIVE_SEL_PC7                = 5,
    /*! Comparator1 positive input selection is PB14
     */
    LL_COMP1_POSITIVE_SEL_PB14               = 6,
    /*! Comparator1 positive input selection is PB12
     */
    LL_COMP1_POSITIVE_SEL_PB12               = 7,
    /*! Comparator1 positive input selection is PB10
     */
    LL_COMP1_POSITIVE_SEL_PB10               = 8,
    /*! Comparator1 positive input selection is PB8
     */
    LL_COMP1_POSITIVE_SEL_PB8                = 9,
    /*! Comparator1 positive input selection is PB6
     */
    LL_COMP1_POSITIVE_SEL_PB6                = 10,
    /*! Comparator1 positive input selection is PB4
     */
    LL_COMP1_POSITIVE_SEL_PB4                = 11,
    /*! Comparator1 positive input selection is PB2
     */
    LL_COMP1_POSITIVE_SEL_PB2                = 12,
    /*! Comparator1 positive input selection is PB0
     */
    LL_COMP1_POSITIVE_SEL_PB0                = 13,
    /*! Comparator1 positive input selection is PA11
     */
    LL_COMP1_POSITIVE_SEL_PA11               = 14,
    /*! Comparator1 positive input selection is PA10
     */
    LL_COMP1_POSITIVE_SEL_PA10               = 15,
    /*! Comparator1 positive input selection is PA9
     */
    LL_COMP1_POSITIVE_SEL_PA9                = 16,
    /*! Comparator1 positive input selection is PA8
     */
    LL_COMP1_POSITIVE_SEL_PA8                = 17,
    /*! Comparator1 positive input selection is PA7
     */
    LL_COMP1_POSITIVE_SEL_PA7                = 18,
    /*! Comparator1 positive input selection is PA6
     */
    LL_COMP1_POSITIVE_SEL_PA6                = 19,
    /*! Comparator1 positive input selection is PA5
     */
    LL_COMP1_POSITIVE_SEL_PA5                = 20,
    /*! Comparator1 positive input selection is PA4
     */
    LL_COMP1_POSITIVE_SEL_PA4                = 21,
    /*! Comparator1 positive input selection is PA3
     */
    LL_COMP1_POSITIVE_SEL_PA3                = 22,
    /*! Comparator1 positive input selection is PA2
     */
    LL_COMP1_POSITIVE_SEL_PA2                = 23,
    /*! Comparator1 positive input selection is PA1
     */
    LL_COMP1_POSITIVE_SEL_PA1                = 24,
    /*! Comparator1 positive input selection is PA0
     */
    LL_COMP1_POSITIVE_SEL_PA0                = 25,
    /*! Comparator1 positive input selection is BG
     */
    LL_COMP1_POSITIVE_SEL_BG                 = 26,
    /*! Comparator1 positive input selection is TEMP
     */
    LL_COMP1_POSITIVE_SEL_TEMP               = 27,
    /*! Comparator1 positive input selection is DAC voltage /4
     */
    LL_COMP1_POSITIVE_SEL_DAC_DIV_4          = 28,
    /*! Comparator1 positive input selection is DAC voltage
     */
    LL_COMP1_POSITIVE_SEL_DAC                = 29,
    /*! Comparator1 positive input selection is OPMA
     */
    LL_COMP1_POSITIVE_SEL_OPMA               = 30,
    /*! Comparator1 positive input selection is OPMB
     */
    LL_COMP1_POSITIVE_SEL_OPMB               = 31,
} TYPE_ENUM_LL_COMP1_POSITIVE_SEL;
 
/***** LL API AND DRIVER API *****/



/**
  * @}
  */

/** @defgroup COMP_LL_Exported_Struct COMP LL Exported Struct
  * @ingroup  COMP_LL_Driver
  * @brief    COMP LL external configuration structure definition
  *
@verbatim   
  ===============================================================================
                                Exported Struct
  ===============================================================================  

    Exported Struct mainly extracts the COMP registers from the API, and abstracts 
    the structure. As long as it implements the low coupling between the registers 
    and the registers, the user only needs to configure the structure of the abstraction 
    layer and call hal_comp_init. Function, you can configure the COMP module without 
    involving the configuration of the collective register.

@endverbatim
  *
  * @{
  */

/**
  * @brief COMP low layer init struct
  */
typedef struct __ll_comp_init {

    /*! comparator0 positive selection
     */
    TYPE_ENUM_LL_COMP0_POSITIVE_SEL         comp0_positive_sel;
    /*! comparator0 negative selection
     */
    TYPE_ENUM_LL_COMP0_NEGATIVE_SEL         comp0_negative_sel;
    /*! comparator1 positive selection
     */
    TYPE_ENUM_LL_COMP1_POSITIVE_SEL         comp1_positive_sel;
    /*! comparator0 or compartor1 invert value is enable or disable
     */
    FunctionalState                         invert_en;
    /*! comparator0 VIN/VIP output  enable
     */
    FunctionalState                         po_en;
    /*! comparator0 low voltage enable
     */
    FunctionalState                         lowp_en;
    /*! comparator0 hysteresis mode selection
     */
    TYPE_ENUM_LL_COMP0_HY_SEL               hy_mode;
    /*! comparator0 or compartor1 filter select N of cycle
     */
    u8                                      filt_num;
} TYPE_LL_COMP_INIT;

/**
  * @brief COMP low layer init struct
  */
typedef struct __ll_comp_wakeup_or_irq_cfg {
    /*! comparator wake up enable or disable
     */
    FunctionalState                         wakeup_en;
    /*! comparator interrupt enable or disable
     */
    FunctionalState                         irq_en;
    /*! comparator set wakeup mode
     */    
    TYPE_ENUM_LL_COMP_WAKEUP_OR_IRQ_MODE    wk_irq_mode;
} TYPE_LL_COMP_WAKEUP_OR_IRQ_CFG;

/**
  * @}
  */

/** @defgroup COMP_LL_Interrupt COMP LL Interrupt Handle function
  * @brief   COMP LL Interrupt Handle function
  *
@verbatim   
  ===============================================================================
                        Interrupt Handle function
  ===============================================================================  

    This subsection provides a set of functions allowing to manage the COMP  
    Interrupt Handle function.

    how to use?

    The COMP interrupt handler uses a callback method that reserves the interface 
    to the user in the form of a callback function. The client needs to initialize 
    the callback function when initializing the COMP in order for the interrupt to 
    be processed normally. 
   
@endverbatim
  *
  * @{
  */



/**
  * @}
  */
  
/** @defgroup COMP_LL_Inti_Cfg COMP LL Initialization And Configuration
  * @brief    COMP LL Initialization And Configuration
  *
@verbatim   
  ===============================================================================
                        Initialization And Configuration
  ===============================================================================  

    This subsection provides a set of functions allowing to manage the COMP data 
    Initialization and Configuration.
    
    how to use?

@endverbatim
  *
  * @{
  */

  /**
  * @brief  comp init
  * @param  channel : enummerate of comparator0 or comparator1
  * @param  p_init  : pointer to the init stuct TYPE_LL_COMP_INIT
  * @retval None
  */
void ll_comp_init(TYPE_ENUM_LL_COMP_CH channel,TYPE_LL_COMP_INIT *p_init);

 /**
  * @brief  comp init
  * @param  channel : enummerate of comparator0 or comparator1
  * @param  p_cfg   : pointer to the init stuct TYPE_LL_COMP_WAKEUP_CFG
  * @retval None
  */
void ll_comp_wakeup_or_irq_config(TYPE_ENUM_LL_COMP_CH channel,TYPE_LL_COMP_WAKEUP_OR_IRQ_CFG *p_cfg);


/**
  * @}
  */
  
/** @defgroup COMP_LL_Data_Transfers COMP LL Data transfers functions
  * @brief    COMP LL Data transfers functions 
  *
@verbatim   
  ===============================================================================
                            Data transfers functions
  ===============================================================================  

    This subsection provides a set of functions allowing to manage the COMP data 
    transfers and receive.
  
@endverbatim
  *
  * @{
  */

 /**
  * @brief  comparator0 interrupt enable
  * @param  None
  * @retval None
  */
void ll_comp0_interrupt_enable(void);

 /**
  * @brief  comparator1 interrupt enable
  * @param  None
  * @retval None
  */
void ll_comp1_interrupt_enable(void);

 /**
  * @brief  comparator0 interrupt disable
  * @param  None
  * @retval None
  */
void ll_comp0_interrupt_disable(void);

 /**
  * @brief  comparator1 interrupt disable
  * @param  None
  * @retval None
  */
void ll_comp1_interrupt_disable(void);

 /**
  * @brief  comparator0 interrupt get enable or disable
  * @param  None
  * @retval result
  */
#define ll_COMP0_INTERRUPT_GET()                        (COMP->CON & LL_COMP_CON0_IE)

 /**
  * @brief  comparator1 interrupt get enable or disable
  * @param  None
  * @retval result
  */
#define ll_COMP1_INTERRUPT_GET()                        (COMP->CON1 & LL_COMP_CON1_IE)

 /**
  * @brief  comparator0 enable
  * @param  None
  * @retval None
  */
void ll_comp0_enable(void);

 /**
  * @brief  comparator1 enable
  * @param  None
  * @retval None
  */
void ll_comp1_enable(void);

 /**
  * @brief  comparator0 disable
  * @param  None
  * @retval None
  */
void ll_comp0_disable(void);

 /**
  * @brief  comparator1 disable
  * @param  None
  * @retval None
  */
void ll_comp1_disable(void);

 /**
  * @brief  comparator0 enable get
  * @param  None
  * @retval result
  */
#define LL_COMP0_EN_GET()                               (COMP->CON & LL_COMP_CON0_EN)

 /**
  * @brief  comparator1 enable get
  * @param  None
  * @retval result
  */
#define LL_COMP1_EN_GET()                               (COMP->CON1 & LL_COMP_CON1_EN)

 /**
  * @brief  comparator0 pending get
  * @param  None
  * @retval result
  */
#define ll_COMP0_PENDING_GET()                          (COMP->STA & LL_COMP_STA_COMP0_PENDING)

 /**
  * @brief  comparator1 pending get
  * @param  None
  * @retval result
  */
#define ll_COMP1_PENDING_GET()                          (COMP->STA & LL_COMP_STA_COMP1_PENDING)

 /**
  * @brief  comparator0 pending clear
  * @param  None
  * @retval None
  */
#define ll_COMP0_PENDING_CLR()                          (COMP->CLR = LL_COMP_STA_COMP0_PENDING)

 /**
  * @brief  comparator1 pending clear
  * @param  None
  * @retval None
  */
#define ll_COMP1_PENDING_CLR()                          (COMP->CLR = LL_COMP_STA_COMP1_PENDING)

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

/**
  * @}
  */

/**
  * @}
  */

#endif //__TS32FX_LL_COMP_H

/*************************** (C) COPYRIGHT 2018 TOPSYS ***** END OF FILE *****/
